package ro.xblue.translator;

import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Hashtable;

import ro.gateway.aida.db.DBPersistenceManager;
import ro.gateway.aida.db.PersistenceToken;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: Module_DB.java,v 1.1 2004/10/24 23:37:02 mihaipostelnicu Exp $
 */
public class Module_DB extends DBPersistenceManager
{
      private Module_DB( PersistenceToken token, String module_name )
      {
            super( token );
            this.messages = new Hashtable();
            this.name = module_name;
            reload();

            make_langs();
      }

      public static Module_DB getManager( PersistenceToken token, String module_name )
      {
            return new Module_DB( token, module_name );
      }

      Hashtable messages;
      String name;

      /**
       * limbile pt. modulul this
       */
      String[] avail_langs;

      public String[] getAvailableLangs()
      {
            return avail_langs;
      }

      private void make_langs()
      {
            this.avail_langs = new String[ messages.size() ];

            Enumeration en = messages.keys();
            int i = 0;
            while ( en.hasMoreElements() )
            {
                  avail_langs[ i++ ] = ( String ) en.nextElement();
            }
      }

      public void reload()
      {
            messages.clear();

            TransItem[] items = null;
            try
            {
                  items = TranslatorDBAccessor.getManager( db_token ).
                        getByModule( name );
            }
            catch ( SQLException ex )
            {
                  ex.printStackTrace();
            }

            if ( items != null )
            {
                  for ( int i = 0; i < items.length; i++ )
                  {
                        Hashtable forlang_items = ( Hashtable ) messages.
                              get( items[ i ].lang );
                        if ( forlang_items == null )
                        {
                              forlang_items = new Hashtable();
                              messages.put( items[ i ].lang, forlang_items );
                        }
                        forlang_items.put( items[ i ].label, items[ i ] );
                  }
            }

            make_langs();
      }

      public String getMessage( String lang, String label )
      {
            Hashtable msgs = ( Hashtable ) messages.get( lang );
            if ( msgs == null ) return null;

            TransItem item = ( TransItem ) msgs.get( label );
            return ( item != null ) ? item.text : label;
      }

      public Hashtable getMessages( String lang )
      {
            if ( lang == null ) return null;
            return ( Hashtable ) messages.get( lang );
      }

      public String getName()
      {
            return name;
      }
}
